package org.apache.lucene.store;

import java.io.IOException;

public class LimitedIndexInput extends IndexInput {
  private long contentLength;
  private long pos = 0;
  private IndexInput input;
  private long startPosition;
  
  public LimitedIndexInput(int contentLength, IndexInput input) {
    startPosition = input.getFilePointer();
    this.contentLength = contentLength;
    this.input = input;
  }
  
  public void close() {}
  
  public void seek(long value) throws IOException {
    input.seek(startPosition + value);
  }
  
  public void readBytes(byte[] b, int off, int len) throws IOException {
    if (pos >= contentLength) {
      return;
    }
    if (pos + len > contentLength) {
      len = (int) (contentLength - pos);
    }
    input.readBytes(b, off, len);
    pos += len;
  }
  
  public long length() {
    return contentLength;
  }
  
  public byte readByte() throws IOException {
    if (pos >= contentLength) {
      return -1;
    }
    pos++;
    return input.readByte();
  }
  
  public long getFilePointer() {
    return pos;
  }
}
